﻿using AntdUI;
using ImportPlcPointPosition.Enums;
using ImportPlcPointPosition.Extensions;
using MiniExcelLibs.Attributes;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImportPlcPointPosition
{
    public class ImportData : AntdUI.NotifyProperty
    {
        public ImportData() {
            _Operate = new AntdUI.CellLink[]
            {
                new AntdUI.CellButton("write","写入",AntdUI.TTypeMini.Primary)
            };
        }

        private AntdUI.CellLink[] _Operate;
        /// <summary>
        /// 操作
        /// </summary>
        public AntdUI.CellLink[] Operate
        {
            get => _Operate;
            set
            {
                _Operate = value;
                OnPropertyChanged(nameof(Operate));
            }
        }

        bool _IsCheck = false;
        /// <summary>
        /// 复选框
        /// </summary>
        public bool IsCheck
        {
            get => _IsCheck;
            set
            {
                if (_IsCheck == value) return;
                _IsCheck = value;
                OnPropertyChanged("IsCheck");
            }
        }

        private int _Id;
        /// <summary>
        /// Id
        /// </summary>
        public int Id
        {
            get { return _Id; }
            set
            {
                _Id = value; 
                OnPropertyChanged(nameof(Id));
            }
        }

        private int _ParentId;
        /// <summary>
        /// ParentId
        /// </summary>
        public int ParentId
        {
            get => _ParentId;
            set
            {
                _ParentId = value;
                OnPropertyChanged(nameof(ParentId));
            }
        }
       
        private string _Description;
        /// <summary>
        /// 描述信息
        /// </summary>
        [ExcelColumnIndex(0)]
        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }

        private string _DataType;
        /// <summary>
        /// 数据类型
        /// </summary>
        [ExcelColumnIndex(0)]
        public string DataType
        {
            get { return _DataType; }
            set { _DataType = value; }
        }


        private decimal _BytePosition;
        /// <summary>
        /// 偏移量 起始位置
        /// </summary>
        [ExcelColumnIndex(2)]
        public decimal BytePosition
        {
            get => _BytePosition;
            set
            {
                _BytePosition = value;
                OnPropertyChanged(nameof(BytePositionStr));
            }
        }

        /// <summary>
        /// 偏移量显示值
        /// </summary>
        public string BytePositionStr
        {
            get => BytePosition.ToString("f1");
        }


        private eEffectType _EffectType;
        /// <summary>
        /// 作用类型 0 常规数据、1订阅数据、2 MES心跳、3 PLC心跳
        /// </summary>
        public eEffectType EffectType
        {
            get { return _EffectType; }
            set { _EffectType = value; }
        }


        private string _WatchValue;
        /// <summary>
        /// 监视值
        /// </summary>
        public string WatchValue
        {
            get => _WatchValue;
            set
            {
                _WatchValue = value;
                OnPropertyChanged("WatchValue");
            }
        }

        private string _WriteValue;
        /// <summary>
        /// 写入值
        /// </summary>
        public string WriteValue
        {
            get { return _WriteValue; }
            set { _WriteValue = value; }
        }

        private bool _AutoWrite = false;
        /// <summary>
        /// 自动写入
        /// </summary>
        public bool AutoWrite
        {
            get { return _AutoWrite; }
            set
            {
                if (_AutoWrite == value) return;
                _AutoWrite = value;
                OnPropertyChanged(nameof(AutoWrite));
            }
        }

        private ImportData[] _SubItems;
        /// <summary>
        /// SubItems
        /// </summary>
        public ImportData[] SubItems
        {
            get => _SubItems;
            set
            {
                if (_SubItems == value) return;
                _SubItems = value;
                OnPropertyChanged(nameof(SubItems));
            }
        }

        private ImportData[] _Children;
        /// <summary>
        /// Children
        /// </summary>
        public ImportData[] Children
        {
            get => _Children;
            set
            {
                if (_Children == value) return;
                _Children = value;
                OnPropertyChanged(nameof(Children));
            }
        }

        /// <summary>
        /// PlcId
        /// </summary>
        public int PlcId { get; internal set; }

        /// <summary>
        /// 工站编号
        /// </summary>
        public string WorkStationCode { get; internal set; }

        /// <summary>
        /// 数据块Id
        /// </summary>
        public int BlockDbId { get; set; }

        /// <summary>
        /// 数据块编号
        /// </summary>
        public int BlockDb { get; set; }

        /// <summary>
        /// 数据块长度
        /// </summary>
        public int BlockLength { get; set; }

        /// <summary>
        /// 转化数据类型
        /// </summary>
        public string ParseType { get; set; }

        /// <summary>
        /// byte长度
        /// </summary>
        public int ByteLength { get; set; }

        /// <summary>
        /// 初始值
        /// </summary>
        [ExcelColumnIndex(3)]
        public string DefaultValue { get; set; } = string.Empty;

        /// <summary>
        /// 模块：1上位机 2AGV
        /// </summary>
        public int Module { get; set; }

        /// <summary>
        /// 位置/组Id
        /// </summary>
        public int LocationId { get; set; }
    
        /// <summary>
        /// 动作编号，仅订阅数据生效，实例化对应的执行操作类 
        /// </summary>
        public int ActionId { get; set; }

        /// <summary>
        /// 动作Key
        /// </summary>
        public string ActionKey { get; set; }

        /// <summary>
        /// 关联动作编号
        /// </summary>
        public int LinkActionId { get; set; }

        /// <summary>
        /// 动作下的步骤编号 
        /// </summary>
        public int StepId { get; set; }

        /// <summary>
        /// 数据编号
        /// </summary>
        public int DataId { get; set; }

        /// <summary>
        /// 数据分类 1 常规读取 2 写入回复信号 ...(可扩展，eg:子篮码、底板码)
        /// 1XXX:读取某种编码 2XXX:写入某种编码
        /// </summary>
        public int DataKind { get; set; } = 1;
        
        /// <summary>
        /// 写入方
        /// </summary>
        public int WriteFrom { get; set; }
      
        /// <summary>
        /// 最后写入位置
        /// </summary>
        public int LastWriteIndex { get; internal set; }

        /// <summary>
        /// TempId
        /// </summary>
        public int TempId { get;  set; }

        /// <summary>
        /// TempParentId
        /// </summary>
        public int TempParentId { get;  set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; } = string.Empty;

        /// <summary>
        /// 排序
        /// </summary>
        public int OrderNo { get; set; }

        #region IsIgnore

        [SugarColumn(IsIgnore = true)]
        public CellButton EffectTypeCellText
        {
            get
            {
                var text = EffectType.GetDisplayDescription();

                var eList = EnumExtensions.ToEnumList<eEffectType>().OrderBy(r => r.GetDisplayOrder()).Select(o =>
                    new AntdUI.SelectItem(o.GetDisplayDescription(), (int)o)
                );

                return new CellButton(this._Id.ToString(), text)
                {
                    Ghost = true,
                    BorderWidth = 1,
                    //ShowArrow = true,
                    Tooltip = "单击下拉选择",
                    DropDownArrow = true,
                    DropDownItems = eList.ToArray(),
                    DropDownMaxCount = 5,
                    DropDownValueChanged = (value) =>
                    {
                        eEffectType badge = (eEffectType)(value);
                        EffectType = badge;
                        OnPropertyChanged("EffectTypeCellText");
                    }
                };
            }
        }

        #endregion
    }
}
